﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;
using System.Windows.Input;
using Granite.Modeling.Internals;
using Granite.Testing;

namespace Granite.Xaml.Tests.Commands
{
	[TestClass]
	public class DelegateCommandTests
	{

		[TestMethod]
		public void DelegateCommand_CtrTest1()
		{

			try
			{
				var x = new DelegateCommand((Action)null);
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentNullException ex)
			{
				Assert.AreEqual("command", ex.ParamName);
			}

		}

		[TestMethod]
		public void DelegateCommand_CtrTest2()
		{

			try
			{
				var x = new DelegateCommand((Action<object>)null);
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentNullException ex)
			{
				Assert.AreEqual("command", ex.ParamName);
			}

		}

		[TestMethod]
		public void DelegateCommand_EventTest1()
		{
			var eventFired = false;
			EventHandler foo = (s, e) => eventFired = true;

			var cmd = new DelegateCommand(x => { }, x => true);
			cmd.CanExecuteChanged += foo;
			Assert.IsFalse(eventFired);

			cmd.OnCanExecuteChanged();
			Assert.IsTrue(eventFired);
			
			eventFired = false;
			cmd.CanExecuteChanged -= foo;
			cmd.OnCanExecuteChanged();
			Assert.IsFalse(eventFired);
		}

		[TestMethod]
		public void DelegateCommand_EventTest2()
		{
			var eventFired = false;
			EventHandler foo = (s, e) => eventFired = true;

			var cmd = new DelegateCommand(x => { }, null);
			cmd.CanExecuteChanged += foo;
			Assert.IsFalse(eventFired);

			cmd.OnCanExecuteChanged();
			Assert.IsFalse(eventFired);

			eventFired = false;
			cmd.CanExecuteChanged -= foo;
			cmd.OnCanExecuteChanged();
			Assert.IsFalse(eventFired);
		}

	}
}
